// KeyNameEdit.cpp : implementation file
//

#include "stdafx.h"
#include "TLBBHelper.h"
#include "KeyNameEdit.h"


// CKeyNameEdit

IMPLEMENT_DYNAMIC(CKeyNameEdit, CEdit)

CKeyNameEdit::CKeyNameEdit()
{
	_key.key	= VK_TAB;
	_key.bCtrl	= true;
}

CKeyNameEdit::~CKeyNameEdit()
{
}


BEGIN_MESSAGE_MAP(CKeyNameEdit, CEdit)
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_CHAR()
	ON_WM_SYSKEYDOWN()
	ON_WM_SYSKEYUP()
	ON_WM_SYSCHAR()
END_MESSAGE_MAP()


void CKeyNameEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	//CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
}

void CKeyNameEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	//CEdit::OnChar(nChar, nRepCnt, nFlags);
}


void CKeyNameEdit::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	OnKeyDown(nChar, nRepCnt, nFlags);
	//CEdit::OnSysKeyDown(nChar, nRepCnt, nFlags);
}

void CKeyNameEdit::OnSysKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	//CEdit::OnSysKeyUp(nChar, nRepCnt, nFlags);
}

void CKeyNameEdit::OnSysChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	//CEdit::OnSysChar(nChar, nRepCnt, nFlags);
}

void CKeyNameEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	_key.key = nChar;

	if (nChar != VK_CONTROL)
		_key.bCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
	else
		_key.bCtrl = true;

	if (nChar != VK_MENU)
		_key.bAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
	else
		_key.bAlt = true;

	UpdateDataText();

	//CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CKeyNameEdit::UpdateDataText()
{
	CString str;
	if (_key.bCtrl)
		str += "<Ctrl>";

	if (_key.bAlt)
		str += str.GetLength() ? "+<Alt>" : "<Alt>";

	CString strCode = _GetKeyString(_key.key);
	str += (str.GetLength() && strCode.GetLength() ) ? "+" + strCode : strCode;

	SetWindowText(str);
}

#if 0
#define VK_NUMPAD0        0x60
#define VK_NUMPAD1        0x61
#define VK_NUMPAD2        0x62
#define VK_NUMPAD3        0x63
#define VK_NUMPAD4        0x64
#define VK_NUMPAD5        0x65
#define VK_NUMPAD6        0x66
#define VK_NUMPAD7        0x67
#define VK_NUMPAD8        0x68
#define VK_NUMPAD9        0x69
#define VK_MULTIPLY       0x6A
#endif

CString CKeyNameEdit::_GetKeyString(int code)
{
	switch (code)
	{
		default:
			{
				if (code >= 0x30 && code <= 0x39)
				{
					char ch = '0' + code - 0x30;
					CString str(ch);
					return str;
				}
				if (code >= 0x41 && code <= 0x5A)
				{
					char ch = 'A' + code - 0x41;
					CString str(ch);
					return str;
				}
				if (code >= VK_F1 && code <= VK_F12)
				{
					int ch = 1 + code - VK_F1;
					CString str;
					str.Format("%d", ch);
					return "F" + str;
				}
			}
			return "";
		case VK_BACK:
			return "<Back>";
		case VK_TAB:
			return "<Tab>";
		case VK_RETURN:
			return "<Return>";
		case VK_SHIFT:
			return "<Shift>";
		case VK_CAPITAL:
			return "<Capital>";
		case VK_ESCAPE:
			return "<Escape>";
		case VK_SPACE:
			return "<Space>";
		case VK_PRIOR:
			return "<Prior>";
		case VK_NEXT:
			return "<Next>";
		case VK_END:
			return "<End>";
		case VK_HOME:
			return "<Home>";
		case VK_LEFT:
			return "<Left>";
		case VK_UP:
			return "<Up>";
		case VK_RIGHT:
			return "<Right>";
		case VK_DOWN:
			return "<Down>";
		case VK_INSERT:
			return "<Insert>";
		case VK_DELETE:
			return "<Delete>";
		case VK_ADD:
			return "<Add>";
		case VK_SEPARATOR:
			return "<Separator>";
		case VK_SUBTRACT:
			return "<Subtract>";
		case VK_DECIMAL:
			return "<Decimal>";
		case VK_DIVIDE:
			return "<Divide>";

		case VK_OEM_1:
			return ";";
		case VK_OEM_PLUS:
			return "=";
		case VK_OEM_COMMA:
			return ",";
		case VK_OEM_MINUS:
			return "_";
		case VK_OEM_PERIOD:
			return ".";
		case VK_OEM_2:
			return "/";
		case VK_OEM_3:
			return "`";

		case VK_OEM_4:
			return "[";
		case VK_OEM_5:
			return "\\";
		case VK_OEM_6:
			return "]";
		case VK_OEM_7:
			return "'";
	}
}
